<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/5/7
 * Time: 17:40
 */
namespace app\order\model;

use think\Db;

/**
 * 订单评审
 * Class Orders
 * @package app\order\model
 */
class Orders extends BaseModel{
    protected $tableName = 'ari_orders';

    /**
     * 获取上次添加的id
     * @return string
     */
    public function lastSql(){
        $lastId = Db::table($this->tableName)->getLastInsID();
        return $lastId;
    }

    /**
     * 批量添加子订单
     * @param $data
     * @return int|string
     */
    public function insertAll($data){
        $result = Db::table($this->tableName)->insertAll($data);
        return $result;
    }

    /**
     * 分页联合查询结果--不适用与主表订单
     * @return array|\think\paginator\Collection
     */
    public function joinAll($where){
        $num = 10;
        $join = array(
            /*['ari_meter_types meter', $this->tableName.'.meter_id = meter.meter_id'],*/
            ['ari_clients cli', $this->tableName.'.cid = cli.cid'],
            ['ari_programs pro', $this->tableName.'.pro_id = pro.pro_id'],
            ['ari_states sta', 'ari_clients.sid=sta.sid', 'LEFT']
        );
        $count = Db::table($this->tableName)->join($join)
            ->where($where)->count();
        $joins = Db::table($this->tableName)->join($join)
            ->where($where)->order("ari_orders.create_time_o desc")->paginate($num, $count, ['query'=>request()->param()]);

        return $joins;
    }

    /**
     * 配件执行
     * @param $where
     * @return \think\paginator\Collection
     */
    public function joinExec($where){
        $num = 10;
        $join = array(
            /*['ari_meter_types meter', $this->tableName.'.meter_id = meter.meter_id'],*/
            ['ari_clients cli', $this->tableName.'.cid = cli.cid'],
            ['ari_programs pro', $this->tableName.'.pro_id = pro.pro_id'],
            ['ari_states sta', 'ari_clients.sid=sta.sid', 'LEFT'],
            ['ari_fittings fit', $this->tableName.'.fid=fit.fid'],
            ['ari_product_principles princ', 'ari_fittings.pid=princ.pid']
        );
        $count = Db::table($this->tableName)->join($join)->where($where)->count();
        $joins = Db::table($this->tableName)->join($join)->where($where)->order("ari_orders.create_time_o desc")->paginate($num, $count, ['query'=>request()->param()]);
        return $joins;
    }
    /**
     * 配件执行--不带分页  20190515
     * @param $where
     * @return \think\paginator\Collection
     */
    public function joinExecNopage($where){
        $join = array(
            /*['ari_meter_types meter', $this->tableName.'.meter_id = meter.meter_id'],*/
            ['ari_clients cli', $this->tableName.'.cid = cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid', 'LEFT'],
            ['ari_fittings fit', $this->tableName.'.fid=fit.fid'],
            ['ari_product_principles princ', 'ari_fittings.pid=princ.pid']
        );
        $joins = Db::table($this->tableName)->join($join)->where($where)->order("ari_orders.create_time_o desc")->select();
        return $joins;
    }

    /**
     * 订单总额
     * @return float|int
     */
    public function sum($condition){
        $sum = Db::table($this->tableName)->sum($condition);
        return $sum;
    }

    /**
     * 分页联合查询
     * @param $field
     * @param $joinTable
     * @param $param
     * @param $where
     * @param $count
     * @return \think\paginator\Collection
     */
    public function join($client, $proId, $status, $orderPlacer){
        $num = 10;
        $where = array('parent_id'=>'0');

        //需要多层判断（如果为空则不执行）
        if($client != ''){
            $where[$this->tableName.'.cid'] = $client;
        }
        if($proId != ''){
            $where[$this->tableName.'.pro_id'] = $proId;
        }
        if($status != ''){
            $where['deliver_status'] = $status;
        }
        if($orderPlacer != ''){
            $where['order_placer'] = ['like', "%$orderPlacer%"];
        }
        $sale_id = session('order_saleid');
        if($sale_id != 0){
            $where['sale_id'] = $sale_id;
        }
        $orders = $this->joinAll($where);
        return $orders;
    }

    /**
     * 不带分页的联合查询
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function joinSelect($where){
        $join = array(
            /*['ari_meter_types meter', $this->tableName.'.meter_id = meter.meter_id'],*/
            ['ari_clients cli', $this->tableName.'.cid = cli.cid'],
            ['ari_programs pro', $this->tableName.'.pro_id = pro.pro_id'],
            ['ari_states sta', 'ari_clients.sid=sta.sid', 'LEFT']
        );
        $joins = Db::table($this->tableName)->join($join)->where($where)->select();
        return $joins;
    }


    /**
     * 配件执行的搜索功能
     * @param $cid
     * @param $proId
     * @param $deliverStatus
     * @param $orderPlacer
     * @param $where
     * @return array|\think\paginator\Collection
     */
    public function searchExec($cid, $proId, $deliverStatus, $orderPlacer, $where){
        if($cid != ''){
            $where[$this->tableName.'.cid'] = $cid;
        }
        if($proId != ''){
            $where[$this->tableName.'.pro_id'] = $proId;
        }
        if($deliverStatus != ''){
            $where['deliver_status'] = $deliverStatus;
        }
        if($orderPlacer != ''){
            $where['order_placer'] = $orderPlacer;
        }
        $orders = $this->joinExec($where);
        return $orders;

    }




}